package com.ukee.birthday.modules.controller;

import com.ukee.birthday.common.annotation.SysLog;
import com.ukee.birthday.common.utils.PageUtils;
import com.ukee.birthday.common.utils.R;
import com.ukee.birthday.common.validator.ValidatorUtils;
import com.ukee.birthday.modules.entity.StaffEntity;
import com.ukee.birthday.modules.service.StaffService;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/staff")
public class StaffController extends AbstractController {

    @Resource
    private StaffService staffService;

    /**
     * 员工一览
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = staffService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 新增员工
     */
    @PostMapping("/save")
    public R save(@RequestBody StaffEntity staff){
        ValidatorUtils.validateEntity(staff);
        if (null != staff.getId() && staff.getId() > 0) {
            staff.setCreateTime(new Date());
            staffService.updateById(staff);
        }
        else {
            staff.setCreateTime(new Date());
            //sha256加密
            String salt = RandomStringUtils.randomAlphanumeric(20);
            //新增员工默认设置
            staff.setPassword(new Sha256Hash("123456", salt).toHex());
            staff.setSalt(salt);
            staffService.save(staff);
        }
        return R.ok();
    }

    /**
     * 员工信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
        StaffEntity staffEntity = staffService.getById(id);
        return R.ok().put("staff", staffEntity);
    }

    @SysLog("删除员工")
    @PostMapping("/delete")
//    @RequiresPermissions("")
    public R delete(@RequestBody Long[] ids){
        staffService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
